package com.voxel.sense.infomsg.module.firm.details.history;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.com.common.utils.TimeUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.HistoryDataListBean;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;
import com.voxel.sense.infomsg.data.bean.RealDataBean;
import cyy.example.com.facotry.constants.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ChenYiYao on 2018/8/1.
 */
public class HistoryRecycleAdapterSpare extends RecyclerAdapter<MultiItemBean> {

    @Override
    protected int getItemViewType(int position, MultiItemBean bean) {
        //小于mElementCount个数的，则是表格数据，否则为图标
        int type = bean.getType();
        int layout = 0;
        switch (type) {
            case Constants.DES_TYPE:
                layout = R.layout.item_real_data_des_head;
                break;
            case Constants.HEAD_TYPE:
            case Constants.NORMAL_TYPE:
                layout = R.layout.item_recycler_history_data;
                break;
        }
        return layout;
    }

    @Override
    protected ViewHolder<MultiItemBean> onCreateViewHolder(View root, int viewType) {
        return null;
    }

    static class NorLineViewHolder extends ViewHolder<MultiItemBean> {
        @BindView(R.id.tv_date_day)
        TextView tv_date_day;//日期月日
        @BindView(R.id.tv_date_hours)
        TextView tv_date_hours; //十分
        @BindView(R.id.ll_element_list)
        LinearLayout ll_element_list; //元素item的父view

        private NorLineViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean tableDataBean) {
            HistoryDataListBean<List<RealDataBean>> bean = (HistoryDataListBean<List<RealDataBean>>) tableDataBean.getData();
            long time = bean.getTime();
            Date date = TimeUtils.millis2Date(time);
            String monthDay = TimeUtils.millis2String(time, new SimpleDateFormat("MM-dd"));
            String HoursMin = TimeUtils.millis2String(time, new SimpleDateFormat("HH:mm"));
            tv_date_day.setText(monthDay);
            tv_date_hours.setText(HoursMin);

            //添加元素
            List<RealDataBean> data = bean.getData();
            for (int i = 0; i < data.size(); i++) {
                TextView textView = ll_element_list.findViewById(i);
                if (textView == null) {
                    Context context = ll_element_list.getContext();//充气
                    textView = (TextView) LayoutInflater.from(context).inflate(R.layout.layout_history_data_textview, ll_element_list);
                    textView.setId(i);
                }
                textView.setVisibility(View.VISIBLE);
                RealDataBean realDataBean = data.get(i);
                textView.setText(realDataBean.getParamName() + "  " + realDataBean.getValue());
            }
            //子view多余真实元数，则隐藏多余的
            if (data.size() < ll_element_list.getChildCount()) {
                for (int i = ll_element_list.getChildCount() - 1; i >= data.size(); i++) {
                    ll_element_list.getChildAt(i).setVisibility(View.GONE);
                }
            }

        }
    }


    static class DesHeadViewHolder extends ViewHolder<MultiItemBean<String>> {
        @BindView(R.id.tv_des)
        TextView tv_des;

        private DesHeadViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean<String> tableDataBean) {
            tv_des.setText(tableDataBean.getData());
        }
    }
}
